<?php
/**
 * @package mikrokosmos
 * mk script file created on 31-mag-2005
 * jaco_at_pixeldump_dot_org
 *
 */

if(!class_exists("MKTagHeader")){ class MKTagHeader {}}


/**
 * movieclip/sprite class
 * it is a wrapper for DefineSprite tag
 *
 * valid control tags are:
 * ShowFrame
 * StartSound
 * PlaceObject
 * FrameLabel
 * PlaceObject2
 * SoundStreamHead
 * RemoveObject
 * SoundStreamBlock
 * RemoveObject2
 * End
 * all SWF 3 Actions
 */
class MKMovieClip  extends MKTagHeader {

	var $tagData		= "";

	// spriteID alias of itemID
	var $frameCount		= 1;
	var $controlTags	= array();
	var $rawControlTags = array();
	var $tagReport;				// "indexed" tag report see: MKTagReport
	var $offsetMap = array();	// these members are useful to improve search/retrieving ...
	var $itemIDMap = array();	// ...  tags by their offset/itemID

	var $workingFrame = 1;


	var $po;			// placeObject2
	var $linkage;		// linkage


	// the constructor
	function MKMovieClip($tagData = "", $spriteID = 1){

		$this->itemID = $spriteID;
		$this->po = new MKPlaceObject2("", $spriteID, $spriteID);

		if(strlen($tagData)){
			$this->tagData = $tagData;
			parent::MKTagHeader(substr($tagData, 0, 10));
			$this->get_movieClip_data();
			return;
		}
	}

	function set_tagData($tagData) { $this->tagData = $tagData; }
	function get_tagData() { return $this->tagData; }

	function set_spriteID($spriteID) { $this->itemID = $spriteID; }
	function get_spriteID(){ return $this->itemID; }

	//
	function get_frameCount(){ return $this->frameCount; }

	//
	function set_controlTags($controlTags){ $this->controlTags = $controlTags; }
	function get_controlTags(){ return $this->controlTags; }

	//
	function set_tagReport($tagReport) { $this->tagReport = $tagReport;	}
	function get_tagReport() { return $this->tagReport;	}

	//
	function set_offsetMap($offsetMap) {$this->offsetMap = $offsetMap; }
	function get_offsetMap() { return $this->offsetMap; }

	//
	function set_itemIDMap($itemIDMap) {$this->itemIDMap = $itemIDMap; }
	function get_itemIDMap() { return $this->itemIDMap; }

	//
	function get_placeDepth(){ return $this->po->get_placeDepth(); }

	//
	function get_placeObject2() { return $this->po; }

	// set translation data for placeObject2
	function move($x, $y, $depthFlag = false, $isChange = false){
		$this->po->set_position($x, $y, $depthFlag, $isChange);
	}

	// set scale data for placeObject2
	function scale($scaleX, $scaleY, $depthFlag = false, $isChange = false){
		$this->po->set_scale($scaleX, $scaleY, $depthFlag, $isChange);
	}

	// set rotation data for placeObject2
	function rotate($rotation, $depthFlag = false, $isChange = false){
		$this->po->set_rotation($rotation, $depthFlag, $isChange);
	}

	//
	function set_depth($depth){ $this->po->set_depth($depth); }
	function get_depth() { return $this->po->get_depth(); }

	//
	function update_linkage($linkageName = ""){
		if(!isset($this->linkage)) $this->linkage = new MKExportAssets();
		$this->linkage->update_asset($this->itemID, $linkageName, 0);

		// prevent empty linkage name
		// in case it provides an automated generated name
		$linkageRef = $this->linkage->get_assetAt(0);
		if(!strlen($linkageRef["name"])) $linkageName = "spriteID" .$this->itemID;
		$this->linkage->update_asset($this->itemID, $linkageName, 0);

		return $this->linkage->update_tagData();
	}

	//
	function set_linkage_name($linkageName = ""){
		return $this->update_linkage($linkageName);
	}

	//
	function set_movieClip_data($mcd = array()){

		if(count($mcd)){
			$this->get_movieClip_data($mcd);
		}
		else {
			$mcd = array();
			$mcd = $this->fetch_assoc();
		}

		$this->tagData = write_movieClip_data($mcd);

		return $this->tagData;
	}

	function get_movieClip_data($mcd = array()){

		if(!count($mcd)){
			$mcd = array();
			$mcd = read_movieclip_data($this->tagData);
		}

		$this->itemID = $mcd["itemID"];
		$this->frameCount = $mcd["frameCount"];
		$this->rawControlTags = $mcd["rawControlTags"];

		$rf = new MKFrameListTranslator($this->rawControlTags);
		$this->controlTags = $rf->rawBody_to_frameList();
		$this->tagReport = $rf->get_tagReport();
		$this->offsetMap = $rf->get_offsetMap();
		$this->itemIDMap = $rf->get_itemIDMap();

		return $mcd;
	}

	//
	function get_tagByOffset($offset){

		$offsetIndex = "x" .$offset;
		if(!isset($this->offsetMap[$offsetIndex])) return false;

		$tagRef = $this->offsetMap[$offsetIndex];
		$frameIndex = $tagRef["frameIndex"];
		$tagIndex = $tagRef["tagIndex"];

		$swfFrame = $this->controlTags[$frameIndex];

		return $swfFrame->get_tagAt($tagIndex);
	}

	//
	function get_tagByItemID($itemID){

		$offsetID = "x" .$itemID;
		if(!isset($this->itemIDMap[$offsetID])) return false;

		$tagRef = $this->itemID[$offsetID];
		$frameIndex = $tagRef["frameIndex"];
		$tagIndex = $tagRef["tagIndex"];

		$swfFrame = $this->controlTags[$frameIndex];

		return $swfFrame->get_tagAt($tagIndex);
	}

	//
	function fetch_assoc(){

		$mcd = array();
		$mcd["itemID"] = $this->itemID;
		$mcd["frameCount"] = $this->frameCount;
		$mcd["rawControlTags"] = $this->rawControlTags;

		return $mcd;
	}

	//
	function fetch_xml($includeHeader = true, $tabLevel = 0){

		$xmlStr = $includeHeader ? XML_HEADER : "";
		$pfx = get_xml_indent_row(++$tabLevel);

		$xmlStr .= $pfx ."<movieClip id=\"";
		$xmlStr .= $this->itemID ."\">";

		$pfx = get_xml_indent_row(++$tabLevel);
		$xmlStr .= $pfx ."<mcTimeline frameCount=\"";
		$xmlStr .= count($this->controlTags) ."\">";
		$i = 1;

		foreach($this->controlTags as $frame){

			if($frame->frame_is_empty()) continue;

			$pfx = get_xml_indent_row(++$tabLevel);
			$xmlStr .= $pfx ."<frameItem number=\"";
			$xmlStr .= $frame->get_frameNumber();
			$xmlStr .= "\">\r\n";

			$tagList = $frame->get_tagList();

			foreach($tagList as $tag){

				$pfx = get_xml_indent_row(++$tabLevel);

				$tagID = $tag->get_tagID();
				$tagType = $tag->get_tagType();
				$buf = $tag->get_tagData();

				$obj = $tag;

				if($tagID == TEXTFIELD){
					$obj = new MKTextField($buf);
				}
				else if($tagType == "DefineFont2"){
					$obj = new MKFontContainer($buf);
				}
				else if($tagID == PLACEOBJECT2){
					$obj = new MKPlaceObject2($buf);
				}
				else if($tagType == "RemoveObject2"){
					$obj = new MKRemoveObject2($buf);
				}
				else if($tagType == "FrameLabel"){
					$obj = new MKFrameLabel($buf);
				}
				else if($tagType == "Protect"){
					$obj = new MKProtect($buf);
				}
				else if($tagID == LINKAGE){
					$obj = new MKExportAssets($buf);
				}
				else if($tagID == SOUND_SH || $tagID == SOUND_SH2){
					$obj = new MKSoundStreamHead($buf);
				}

				$xmlStr .= $obj->fetch_xml(false, $tabLevel);

				$pfx = get_xml_indent_row(--$tabLevel);
			}

			$pfx = get_xml_indent_row(--$tabLevel);
			$xmlStr .= $pfx ."</frameItem>\r\n";
		}

		$pfx = get_xml_indent_row(--$tabLevel);
		$xmlStr .= $pfx . "</mcTimeline>\r\n";
		$pfx = get_xml_indent_row(--$tabLevel);
		$xmlStr .= $pfx . "</movieClip>";

		return $xmlStr;
	}


	//
	function add_tagData($swfTag, $frameNumber = 1){

		if(!$frameNumber) $frameNumber = $this->workingFrame;

		$fi = $frameNumber - 1;				// frame index

		if(!isset($this->rawControlTags[$fi])){ // start new tagList in case of missing
			$this->rawControlTags[$fi] = array();
			$ti = 0;
		}
		else  $ti = count($this->rawControlTags[$fi]);

		$this->rawControlTags[$fi][$ti] = $swfTag;
		$this->frameCount = count($this->rawControlTags);
	}

	//
	function add_frameData($frameAmount = -1){

		$rcti = count($this->rawControlTags);
		$this->rawControlTags[$rcti] = array();

		if($frameAmount > 0){
			for($i = 1; $i < $frameAmount; $i++){
				$rcti = count($this->rawControlTags);
				$this->rawControlTags[$rcti] = array();
			}
		}

		$this->workingFrame = count($this->rawControlTags);

		return $this->workingFrame;
	}

	//
	function update_tagData($cctCompile = OUTPUT_FROM_FRAMELIST_SIMPLE){
		return $this->compile_controlTags($cctCompile);
	}

	// returns placeObject2 tagData
	function update_place_tagData($replacingDepth = -1){
		return $this->po->update_tagData($replacingDepth);
	}

	// returns linkage tagData
	function update_linkage_tagData(){
		$this->update_linkage();
		return $this->linkage->update_tagData();
	}

	//
	function compile_controlTags($cctCompile){

		$mcBody = swf_word($this->itemID);
		$mcBody .= swf_word($this->frameCount);

		if($cctCompile = OUTPUT_FROM_FRAMELIST_SIMPLE){

	 		if(!count($this->rawControlTags)) {
	 			$mcBody .= swf_showFrame();
	 		}
	 		else {
	 	  		foreach($this->rawControlTags as $tagList){

	 				foreach($tagList as $tag) $mcBody .= $tag;

	 				$mcBody .= swf_showFrame();
	 	  		}
	 	  	}

			$mcBody .= SWFTag_end();
		}

		$mcbLength = strlen($mcBody);
		$mcHead = SWFTag_long_header(MOVIECLIP, $mcbLength);

		$this->tagData = $mcHead .$mcBody;

		return $this->tagData;
	}
}
?>
